// 模拟与后端API通信的函数
// 在实际项目中，这些函数将使用fetch或axios等工具与真实的后端API通信

import { getCurrentUser } from "./auth"

// 用户信息相关API
export async function fetchUserProfile() {
  // 模拟 API 请求获取用户个人资料
  return new Promise((resolve) => {
    setTimeout(() => {
      const user = getCurrentUser()
      resolve({
        email: user?.email || "demo@example.com",
        realname: "张三",
        phone: "13800138000",
        avatar: "/images/profilePhoto.jpg",
      })
    }, 600)
  })
}

export async function updateUserProfile(data: any) {
  // 模拟 API 请求更新用户个人资料
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("用户资料更新为:", data)
      resolve({ success: true })
    }, 800)
  })
}

export async function fetchUserData() {
  // 模拟 API 请求获取用户信息
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        id: "1001",
        name: "张三",
        email: "demo@example.com",
        signatureCount: 3,
        lastLogin: "2025-02-28 08:45:00",
      })
    }, 600)
  })
}

export async function fetchRecentActivities() {
  // 模拟 API 请求获取最近活动
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          id: "1",
          date: "2025-02-28 14:34",
          description: "更新了个人信息",
        },
        {
          id: "2",
          date: "2025-02-25 08:16",
          description: "上传了新的签名样本",
        },
        {
          id: "3",
          date: "2025-02-20 12:31",
          description: "成功认证了签名真伪",
        },
      ])
    }, 700)
  })
}

// 签名相关API
export async function fetchSignatures() {
  // 模拟 API 请求获取用户的签名列表
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          id: "1",
          imageUrl: "/images/signature1.png",
          createdAt: "2025-02-10 09:30:00",
        },
        {
          id: "2",
          imageUrl: "/images/signature2.png",
          createdAt: "2025-02-12 15:45:00",
        },
        {
          id: "3",
          imageUrl: "/images/signature3.png",
          createdAt: "2025-02-15 11:20:00",
        },
      ])
    }, 800)
  })
}

export async function uploadSignature(file: File) {
  // 模拟 API 请求上传签名图片
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("签名上传成功:", file.name)
      resolve({ success: true, id: "new-sig-" + Date.now() })
    }, 1200)
  })
}

export async function deleteSignature(id: string) {
  // 模拟 API 请求删除签名
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("签名删除成功:", id)
      resolve({ success: true })
    }, 600)
  })
}

export async function verifySignature(file: File) {
  // 模拟 API 请求验证签名
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("验证签名:", file.name)

      // 随机生成验证结果，实际情况会根据真实的比对结果返回
      const threshold = 0.15
      const distance = Math.random() * 0.3
      const match = distance < threshold

      resolve({
        threshold,
        distance,
        match,
      })
    }, 2000)
  })
}

// 管理员相关API
export async function fetchUsers() {
  // 模拟 API 请求获取用户列表
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          id: "1001",
          username: "张三",
          email: "zhangsan@example.com",
          role: "user",
          status: "active",
        },
        {
          id: "1002",
          username: "李四",
          email: "lisi@example.com",
          role: "admin",
          status: "frozen",
        },
        {
          id: "1003",
          username: "王五",
          email: "wangwu@example.com",
          role: "user",
          status: "active",
        },
      ])
    }, 700)
  })
}

export async function updateUserRole(userId: string, role: "admin" | "user") {
  // 模拟 API 请求更新用户角色
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`用户 ${userId} 角色更新为 ${role}`)
      resolve({ success: true })
    }, 600)
  })
}

export async function freezeUser(userId: string) {
  // 模拟 API 请求冻结用户
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`用户 ${userId} 已被冻结`)
      resolve({ success: true })
    }, 600)
  })
}

export async function unfreezeUser(userId: string) {
  // 模拟 API 请求解冻用户
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`用户 ${userId} 已被解冻`)
      resolve({ success: true })
    }, 600)
  })
}

export async function fetchAuditLogs() {
  // 模拟 API 请求获取审计日志
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          timestamp: "2025-02-20 08:45:00",
          adminId: "admin_001",
          actionType: "权限修改",
          targetUser: "ID: 1001",
          details: '将用户权限从"管理员"修改为"普通用户"',
        },
        {
          timestamp: "2025-02-20 09:30:00",
          adminId: "admin_001",
          actionType: "权限修改",
          targetUser: "ID: 1003",
          details: '将用户权限从"管理员"修改为"普通用户"',
        },
        {
          timestamp: "2025-02-20 10:00:00",
          adminId: "admin_001",
          actionType: "账户冻结",
          targetUser: "ID: 1002",
          details: "冻结用户账户",
        },
      ])
    }, 800)
  })
}
